package jeu.terrains
{
	import away3d.containers.ObjectContainer3D;
	
	
	
	public class PanneauHex extends ObjectContainer3D
	{
		private var _pieces:Array = new Array();
		
		private var _niveau:int;
		private var _padding:int;
		
		
		private var _panneauW:int;
		private var _panneauH:int;
		private var _pieceW:int;
		private var _pieceH:int;
		
		private const UNITE:int = 100;
		
		private const listeType:Array = new Array(
			CatanHexagone.HEXA_DESERT, CatanHexagone.HEXA_FORET, CatanHexagone.HEXA_FORET, CatanHexagone.HEXA_FORET, CatanHexagone.HEXA_FORET, 
			CatanHexagone.HEXA_PATURAGE, CatanHexagone.HEXA_PATURAGE, CatanHexagone.HEXA_PATURAGE, CatanHexagone.HEXA_PATURAGE, CatanHexagone.HEXA_CHAMPS,
			CatanHexagone.HEXA_CHAMPS, CatanHexagone.HEXA_CHAMPS, CatanHexagone.HEXA_CHAMPS, CatanHexagone.HEXA_COLLINE, CatanHexagone.HEXA_COLLINE,
			CatanHexagone.HEXA_COLLINE, CatanHexagone.HEXA_MONTAGNE, CatanHexagone.HEXA_MONTAGNE, CatanHexagone.HEXA_MONTAGNE
		);
		// La liste fixe pour enregistrer les types de terrain
		//Ne change pas
		private var listeTypeTriee:Array = trierParHasard(listeType);
		// La liste triee pour les types de terrain
		
		private const listeTerr:Array = new Array(5,2,6,3,8,10,9,12,11,4,8,10,9,4,5,6,3,11);
		// La liste fixe pour enregistrer les valeurs de de
		//Ne change pas
		
		private function trierParHasard(listeAncienne:Array):Array
		{

			var nombre:int = listeAncienne.length;
			var listeNouvelle:Array = new Array(nombre);
			var listeJugement:Array = new Array(nombre);
			var temp:Number = 0;
			

			
			for (var w:int = 0; w < nombre;w++)
			{
				listeJugement[w] = false;
			}
			
			for (var i:int = 0; i < nombre;i++)
			{
				trace (i);
				trace (temp);
				
				temp = Math.floor(Math.random()*nombre);
				trace (temp);
				
				if (listeJugement[temp]==false)
				{
					listeJugement[temp]==true;
					listeNouvelle[i]=listeAncienne[temp];
					
				}
				else
				{
					i=i-1;
					
				}
			}
			
			
			return listeNouvelle;
		}
		

		
		public function PanneauHex(panneauW:int, panneauH:int, pieceW:int, pieceH:int, niveau:int = 3, padding:int = 2)
		{
			super();
			
			_panneauW = panneauW;
			_panneauH = panneauH;
			_pieceW = pieceW;
			_pieceH = pieceH;
			
			_niveau = niveau;
			_padding = padding;
			
			creerPanneau();
			
		}
		
		public function creerPanneau():void
		{
			viderPanneau();
			
			var k1:int = 0;
			var k2:int = 0;
			var valeurTypeRes:String;
			var valeurTerrain:int;
			
			for(var i:int = 0; i < _niveau*2+1; i++) 
			{
				_pieces[i] = new Array(_niveau*2+1-Math.abs(i-_niveau));
				
				for(var j:int = 0; j < _niveau*2+1-Math.abs(i-_niveau); j++) 
				{
					if ((i==0)||(j==0)||(i==_niveau*2)||(j==_niveau*2+1-Math.abs(i-_niveau)-1))
						// Si c'est le bord => mers
					{
						valeurTypeRes = CatanHexagone.HEXA_MERS;
					}
					else
						// Pas bord => donner un type
					{
						valeurTypeRes = listeTypeTriee[k1];
						if (valeurTypeRes != CatanHexagone.HEXA_DESERT)
							// Si ce n'est pas le desert
						{
							valeurTerrain = listeTerr[k2];
							k2 = k2 + 1;
						}
						else
							// Sinon
						{
							valeurTerrain = 0;
						}
						
						k1 = k1 + 1;
					}
					
					
					var piece:CatanHexagone = new CatanHexagone(UNITE, j*_pieceW+j*_padding, -(i*_pieceH+i*_padding),valeurTypeRes, valeurTerrain);
					
					//piece.addEventListener(CatanJeuEvent.CLICK, clickHandler);
					
					//piece.addEventListener(CatanJeuEvent.MOVE, moveHandler);
					//piece.addEventListener(CatanJeuEvent.READY, moveHandler);
					
					piece.x = (Math.abs(i-_niveau)/2+j) * _pieceW + j * _padding/*(100/200)*UNITE*(Math.abs(i-_niveau)+j-1)+ j * _padding*/;
					piece.z = -(i * _pieceH * 3/4 + i * _padding)/*-(200/200)*UNITE*i+ i * _padding*/;
					
					addChild(piece);
					_pieces[i][j] = piece;
				}
			}
			
			centrerPanneau();
		
			
		}
		
		
		
		
		
		
		/*
		private function clickHandler(e:CatanJeuEvent):void 
		{
			// Click and Move
			if (_moving)
			{
				return ;
			}
		}
		*/
		
		/*
		private function moveHandler(e:CatanJeuEvent):void 
		{
			if (e.type == PuzzleEvent.MOVE)
			{
				_moving = true;
			}
			else 
			if (e.type == PuzzleEvent.READY)
			{
				_moving = false;
			}
		}
		
		*/
		
		private function viderPanneau():void 
		{
			for each(var p:CatanHexagone in _pieces) 
			{
				//p.removeEventListener(PuzzleEvent.CLICK, clickHandler);
				//p.removeEventListener(PuzzleEvent.MOVE, moveHandler);
				//p.removeEventListener(PuzzleEvent.READY, moveHandler)
				removeChild(p);
			}
			_pieces = new Array();
		}
		
		private function centrerPanneau():void 
		{
			this.x = -(_panneauW * .5 - _pieceW * .5);
			this.z = _panneauH * .5 - _pieceH * .5;
		}


		
		
	}
}